package com.hyfrogx.modules.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.ShiroUtils;
import com.hyfrogx.modules.manage.dao.HyAdDao;
import com.hyfrogx.modules.manage.dao.HyChannelApkDao;
import com.hyfrogx.modules.manage.entity.HyChannelApkEntity;
import com.hyfrogx.modules.manage.form.channel.ChannelForm;
import com.hyfrogx.modules.manage.form.channel.ChannelVo;
import com.hyfrogx.modules.manage.service.HyAdService;
import com.hyfrogx.modules.manage.service.HyChannelApkService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/9/24 16:28
 * @Company 汉咏科技
 */
@Service
public class HyChannelApkServiceImpl extends ServiceImpl<HyChannelApkDao, HyChannelApkEntity> implements HyChannelApkService {

    @Autowired
    HyAdService hyAdService;



    @Override
    public Boolean insertChannel(ChannelForm channelForm) {
        //把表单信息转化成实体类
        HyChannelApkEntity hyChannelApkEntity = BeanUtil.toBean(channelForm,HyChannelApkEntity.class);
        //通过token获取登录者id
        Long userid = ShiroUtils.getUserEntity().getUserId();
        //设置创建人id和创建时间
        hyChannelApkEntity.setUid(userid);
        hyChannelApkEntity.setCreateDate(new Date());
        return this.save(hyChannelApkEntity);
    }

    @Override
    public Boolean updateChannel(ChannelForm channelForm) {
        //把表单信息转化成实体类
        HyChannelApkEntity hyChannelApkEntity = BeanUtil.toBean(channelForm,HyChannelApkEntity.class);
        //通过token获取登录者id
        Long userid = ShiroUtils.getUserEntity().getUserId();
        //设置更改人id和更改时间
        hyChannelApkEntity.setUid(userid);
        hyChannelApkEntity.setUpdateDate(new Date());
        return this.updateById(hyChannelApkEntity);
    }

    @Override
    public IPage<ChannelVo> getChannelPage(Map<String, Object> cmap) {
        QueryExt queryExt = new QueryExt(cmap);
        Map map = new HashMap();
        map.putAll(queryExt.getCondition());
        List<HyChannelApkEntity> list = this.baseMapper.selectPageByMap(queryExt,map);
        //把list转换成ChannelVolist
        List<ChannelVo> channelVoList = new ArrayList<>();
        if (list!= null && list.size()>0)
        {
            for (HyChannelApkEntity hyChannelApkEntity :list)
            {
                channelVoList.add(BeanUtil.toBean(hyChannelApkEntity,ChannelVo.class));
            }
        }
        queryExt.setRecords(channelVoList);
        return queryExt;
    }

    @Override
    public Boolean updateStatus(Integer id) {
        ChannelApkUingOrNo(id);
        //根据渠道id查询渠道状态
        Integer status = this.baseMapper.getStatusById(id);
        //创建渠道实体
        HyChannelApkEntity hyChannelApkEntity = new HyChannelApkEntity();
        hyChannelApkEntity.setId(id);
        //判断原状态，赋值与原状态相反的代表数
        status = (status==1) ? 0:1;
        hyChannelApkEntity.setStatus(status);
        return this.updateById(hyChannelApkEntity);
    }

    @Override
    public Boolean deleteChannel(Integer id) {
        ChannelApkUingOrNo(id);
        return  this.removeById(id);
    }

    /**
     * 判断渠道是否使用中
     */
    public void ChannelApkUingOrNo(Integer id)
    {
        HyChannelApkEntity hyChannelApkEntity = this.getById(id);
        Integer channelNum = hyChannelApkEntity.getChannelNum();
        // TODO: 2019/9/25
        //根据渠道号判断，渠道是否正在使用
        //正在使用，返回删除失败
        String str = String.valueOf(channelNum);
        List<String> list = hyAdService.selectByParam(str);
        if (list.size()>0)
        {
            throw new MyException("渠道正在使用");
        }
    }

    @Override
    public List getChannelListByVodId(Integer id) {
        return this.baseMapper.getChannelListByVodId(id);
    }

}
